package cn.ccit.DAOImp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import cn.ccit.util.MyDBUtil;
import cn.ccit.DAO.DiningTableDAO;
import cn.ccit.entites.DiningTable;

public class DiningTableDAOimp implements DiningTableDAO{
	private Session session;
	private static final Log log = LogFactory.getLog(DiningTable.class);
	@Override
	public boolean addDiningTable(DiningTable dt) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(dt);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("add diningtabel fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean removeDiningTable(DiningTable dt) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(dt);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove diningtabel fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean changeDiningTableInfo(DiningTable dt) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(dt);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("change diningtabelInfo fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public List<DiningTable> getSomeDiningTables(int start, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from DiningTable as diningTable order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from DiningTable").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomeDiningTables fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public DiningTable searchDiningTableByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from DiningTable as diningTable where diningTable.id=?";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				DiningTable diningTable=(DiningTable)query.list().get(0);
				session.beginTransaction().commit();
				return diningTable;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search diningtabel by id fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public DiningTable searchDiningTableByName(String name) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from DiningTable as diningTable where diningTable.name=?";
				Query query=session.createQuery(hql);
				query.setString(0, name);
				DiningTable diningTable=(DiningTable)query.list().get(0);
				session.beginTransaction().commit();
				return diningTable;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search diningtabel by name fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public boolean changeDiningTableState(DiningTable dt) {
		boolean flag=false;
		try {
			flag=MyDBUtil.changeDataEnum("DiningTable", dt);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			flag=false;
		}
		return flag;
	}

}
